package com.lagou.edu.service.impl;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Service;
import com.lagou.edu.annotation.Transactional;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.dao.impl.JdbcAccountDaoImpl;
import com.lagou.edu.factory.BeanFactory;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
import com.lagou.edu.utils.ConnectionUtils;
import com.lagou.edu.utils.TransactionManager;

import java.sql.Connection;

/**
 * @author 应癫
 */
@Service("transferService")
@Transactional
public class TransferServiceImpl implements TransferService {

//    private AccountDao accountDao = new JdbcAccountDaoImpl();
//    private AccountDao accountDao = (AccountDao) BeanFactory.getInstance().getBean("accountDao");
//    private AccountDao accountDao = (AccountDao) BeanFactory.getBean("accountDao");
//    private TransactionManager transactionManager;
//
//    public void setTransactionManager(TransactionManager transactionManager) {
//        this.transactionManager = transactionManager;
//    }
   // 最佳状态
    @Autowired
    private AccountDao accountDao;

    // 构造函数传值/set方法传值
//
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }



    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        try{
                System.out.println("进入业务逻辑方法");
                Account from = accountDao.queryAccountByCardNo(fromCardNo);
                Account to = accountDao.queryAccountByCardNo(toCardNo);

                from.setMoney(from.getMoney()-money);
                to.setMoney(to.getMoney()+money);

                accountDao.updateAccountByCardNo(to);
//                int c = 1/0;//模拟报错
                accountDao.updateAccountByCardNo(from);


        }catch (Exception e) {
            e.printStackTrace();
            throw e;

        }




    }
}
